package org.vaadin.vaadinjit.widgetset.client.ui;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONObject;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.UIDL;

public class VJitSpaceTree extends VJitWidget<JitSpaceTreeOverlay> {


	public static final String NODE_CLICK_IDENTIFIER = "nodeClick";


	@Override
	public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
		super.updateFromUIDL(uidl, client);
		if (uidl.hasAttribute("treeOrient")) {
			String treeOrient = uidl.getStringAttribute("treeOrient");
			getVisualization().switchPosition(treeOrient);
		}
		if (uidl.hasAttribute("treeAlign")) {
			String treeAlign = uidl.getStringAttribute("treeAlign");
			getVisualization().switchAlignment(treeAlign);
		}
	}
	
	@Override
	protected native JitSpaceTreeOverlay initJit(JavaScriptObject jitConfig) /*-{
		var elName = jitConfig.injectInto;
	
		jitConfig['onCreateLabel'] = function(label, node){
        	label.id = node.id;            
            label.innerHTML = node.name;
            label.onclick = function(){
           		//st.onClick(node.id);
           		$wnd.$jit.fireSpaceTreeClick(elName, node.id);


            };
            //set label styles
            //var style = label.style;
            //style.width = 60 + 'px';
            //style.height = 17 + 'px';            
            //style.cursor = 'pointer';
            //style.color = '#333';
            //style.fontSize = '0.8em';
            //style.textAlign= 'center';
            //style.paddingTop = '3px';
        };
        jitConfig['transition'] = $wnd.$jit.Trans.Quart.easeInOut;
		
		
		
		return new $wnd.$jit.ST(jitConfig);
	
	}-*/;

	
	@Override
	protected void setupConfig(JitConfiguration config) {
		super.setupConfig(config);
		
		JitConfiguration navigation = new JitConfiguration();
		navigation.setOption("enable", true);
		navigation.setOption("panning", true);
		config.setOption("Navigation", navigation);
		
		JitConfiguration node = new JitConfiguration();
		node.setOption("height", 20);
		node.setOption("width", 60);
		node.setOption("type", "rectangle");
		node.setOption("color", "#fff");
		node.setOption("overridable", true);
		config.setOption("Node", node);
		
		JitConfiguration edge = new JitConfiguration();
		edge.setOption("type", "bezier");
		edge.setOption("overridable", true);
		config.setOption("Edge", edge);
		
		JitConfiguration label = new JitConfiguration();
		label.setOption("textAlign", "center");
		config.setOption("Label", label);
	}
	
	
	@Override
	protected void loadJitData(JSONObject data) {
		 getVisualization().loadJSON(data.getJavaScriptObject());
		 getVisualization().compute();
		 getVisualization().doRootNodeClick();
	}
	
	
	/**
	 * 
	 * @param nodeId
	 */
	public void onClickNode(String nodeId) {
		getVisualization().onClick(nodeId);
		client.updateVariable(paintableId, NODE_CLICK_IDENTIFIER, nodeId, true);
	}

}
